
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;

using Swaf;
using Swaf.BizRule;

namespace Swaf.DataAccess
{
	class BasicTransaction : ITransaction, ITransactionManager
	{
		protected IDictionary m_participants = new Hashtable();
		protected IDictionary m_connections = new Hashtable();
		protected int m_rollbackVotes = 0;
		protected int m_commitVotes = 0;
		protected IList m_voters = new ArrayList();
		TransactionState m_state = TransactionState.notSet;

		#region ITransaction Members

		public Swaf.DataAccess.TransactionState State
		{
			get
			{
				return m_state;
			}
		}

		public void voteRollBack()
		{
			Interlocked.Increment(ref m_rollbackVotes);
			IRunningRule r = ((IRuleManagerEx)Application.currentApp.ruleMgr).CurrentThreadRunningRule;
			if(r != null)
				m_voters.Add(r.UniqueId);
		}

		public void voteCommit()
		{
			Interlocked.Increment(ref m_commitVotes);
			IRunningRule r = ((IRuleManagerEx)Application.currentApp.ruleMgr).CurrentThreadRunningRule;
			if(r != null)
				m_voters.Add(r.UniqueId);
		}

		public void undoRollbackVote()
		{
			if(m_rollbackVotes > 0)
				Interlocked.Decrement(ref m_rollbackVotes);
			else
			{
				//TODO: Throw an exception if we had no rollback votes to undo.
			}
		}

		#endregion

		#region ITransactionManager Members

		public IConnectionItem getConnection(string key)
		{
			return (IConnectionItem)m_connections[key];
		}

		public void addConnection(string key, IConnectionItem conn)
		{
			m_connections[key] = conn;
		}

		public int Participants
		{
			get
			{
				lock(m_participants)
				{
					return m_participants.Count;
				}
			}
		}

		public void addParticipant(object p)
		{
			lock(m_participants)
			{
				m_participants[p] = p;
			}
		}

		public void removeParticipant(object p)
		{
			lock(m_participants)
			{
				m_participants.Remove(p);
				if(m_participants.Count == 0)
					completeTransaction(0);
			}
		}
		public bool hasIdentityVoted(object id)
		{
			Debug.Assert(id is long);
			long i = (long)id;
			foreach(long voterId in m_voters)
				if(voterId == i)
					return true;
			return false;
		}

		#endregion

		protected virtual void completeTransaction(int force)
		{
			bool doRollback = false;

			if((m_rollbackVotes > 0 && force <= 0) || force < 0)
				doRollback = true;
			foreach(IConnectionItem conn in m_connections.Values)
			{
				if(doRollback)
					conn.rollback();
				else
					conn.commit();
				conn.forceClose();
			}
		}
	}

}